//
// Created by AssAs on 2024/9/27.
//

#include "freertos/FreeRTOS.h"
#include <freertos/task.h>
#include <freertos/projdefs.h>
#include <FreeRTOSConfig.h>
#include <soc/gpio_num.h>
#include <portmacro.h>
#include <driver/gpio.h>
#include "circuit_board.h"

#include <audio_tone_uri.h>
#include <my_led_strip.h>
/*--------------------------parameters-----------------------*/
gpio_num_t BUTTON_FUNCTION = GPIO_NUM_0;
gpio_num_t BUTTON_MODE = GPIO_NUM_38;
gpio_num_t SHUTDOWN_GPIO = GPIO_NUM_40;

extern uint32_t last_action_time;

uint32_t shutdown_countdown_time = 1000 * 60 * 60; // 默认10分钟后关闭
/*--------------------------parameters-----------------------*/

void circuit_board_task(void *para) {
    while (1) {
        if (pdTICKS_TO_MS(xTaskGetTickCount()) > last_action_time + shutdown_countdown_time) {
            circuit_board_ShutDown();
        }
        vTaskDelay(100 / portTICK_PERIOD_MS);
    }
}

void circuit_board_task_init() {
    xTaskCreate(circuit_board_task, "circuit_board_task", 1024 * 1, NULL, 6, NULL);
}

void circuit_board_refresh_CountDown() {
    last_action_time = pdTICKS_TO_MS(xTaskGetTickCount());
}

void circuit_board_ShutDown() {

#ifdef TOY_PEANUT
    gpio_set_level(SHUTDOWN_GPIO, 0);
#endif

    vTaskDelay(4000);
}


void led_func_inti() {
    xTaskCreatePinnedToCore(led_task, "led_task", 1024 * 3, NULL, 5, NULL, 1);
}

void circuit_board_init() {

#ifdef TOY_PEANUT
    BUTTON_FUNCTION = GPIO_NUM_41;
    BUTTON_MODE = GPIO_NUM_0;
    SHUTDOWN_GPIO = GPIO_NUM_40;

    //用于软关机的引脚， 该设备该引脚需要默认为高电平。
    gpio_config_t io_conf = {};
    io_conf.intr_type = GPIO_INTR_DISABLE;
    io_conf.mode = GPIO_MODE_OUTPUT;
    io_conf.pin_bit_mask = (1ULL << SHUTDOWN_GPIO);
    io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    gpio_config(&io_conf);
    gpio_set_level(SHUTDOWN_GPIO, 1);
#endif


    circuit_board_task_init();

}

